//
//  XMLVisitorView.swift
//  Weibo
//
//  Created by mac on 16/5/11.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit

class XMLVisitorView: UIView {
    
    var callBackClosure :(() -> ())?
    
    private lazy var cycleImageView :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var maskImageView :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    private lazy var iconImageView :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    private lazy var messageLable :UILabel = {
    
        let lable = UILabel()
        lable.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        lable.textColor = UIColor.darkGrayColor()
        lable.font = UIFont.systemFontOfSize(14)
        lable.textAlignment = .Center
        lable.numberOfLines = 0
        
        return lable
    }()
    private lazy var resigterButton :UIButton = {
    
        let button = UIButton()
        button.setTitle("注册", forState: .Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        button.addTarget(self, action: "resigterButtonClick", forControlEvents: .TouchUpInside)
        
        return button
    }()
    private lazy var loginButton :UIButton = {
        
        let button = UIButton()
        button.setTitle("登录", forState: .Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        button.addTarget(self, action: "loginButtonClick", forControlEvents: .TouchUpInside)
        
        return button
    }()
    
    @objc private func resigterButtonClick() {
    
        callBackClosure?()
    }
    @objc private func loginButtonClick() {
    
        callBackClosure?()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI() {
    
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        addSubview(cycleImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLable)
        addSubview(resigterButton)
        addSubview(loginButton)
        
        
        cycleImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cycleImageView)
        }
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cycleImageView)
        }
        messageLable.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(cycleImageView)
            make.top.equalTo(cycleImageView.snp_bottom)
            make.size.equalTo(CGSize(width: 224, height: 40))
            
        }
        
        resigterButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(messageLable.snp_bottom).offset(10)
            make.leading.equalTo(messageLable)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        loginButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(messageLable.snp_bottom).offset(10)
            make.trailing.equalTo(messageLable)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        
    }
    
    private func startAnimation() {
    
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * M_PI
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        animation.removedOnCompletion = false
        cycleImageView.layer.addAnimation(animation, forKey: nil)
    }
    
    func modifyVisitorInfo(title :String?, imageName :String?) {
    
        if let t = title, imgName = imageName {
        
            messageLable.text = t
            iconImageView.image = UIImage(named: imgName)
            cycleImageView.hidden = true
        } else {
        
            cycleImageView.hidden = false
            startAnimation()
            
        }
    }
    
}
